package com.road7.mina.withprotobuf;

import java.util.List;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import com.road.yishi.pb.active.StudentMsgProto;

/**
 * @Description :
 * @FileName: Serverhandler.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年11月20日 下午5:39:33
 * @Version:V1.00
 */
public abstract class AbstractHandler extends IoHandlerAdapter {

	private String hanldName;

	AbstractHandler(String hanldName) {
		this.hanldName = hanldName;
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {

		System.out.println("进入：" + hanldName);

		// 读取客户端传过来的Student对象
		StudentMsgProto.StudentRspMsg student = (StudentMsgProto.StudentRspMsg) message;
		System.out.println("ID:" + student.getId());
		System.out.println("Name:" + student.getName());
		System.out.println("Email:" + student.getEmail());
		System.out.println("Friends:");
		List<String> friends = student.getFriendsList();
		for (String friend : friends) {
			System.out.println(friend);
		}

		// 新建一个Student对象传到客户端
		StudentMsgProto.StudentRspMsg.Builder builder = StudentMsgProto.StudentRspMsg.newBuilder();
		builder.setId(9);
		builder.setName("服务器");
		builder.setEmail("123@abc.com");
		builder.addFriends("X");
		builder.addFriends("Y");
		StudentMsgProto.StudentRspMsg student2 = builder.build();
		session.write(student2);

	}

}
